package com.yxkj.syh.app.huarong.adps;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.Goods;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者：SYH
 * 时间：2020/5/26 15:26
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class GoodsListAdp extends RecyclerView.Adapter<GoodsListAdp.ViewHolder> {
    List<Goods> data = new ArrayList<>();
    private OnClick mOnClick;
    private String preSelectedGoodsCode;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Goods goods = data.get(position);
        holder.tvName.setText(goods.getName());
        switch (goods.getLableStatus()) {
            case 0:
                holder.tvName.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg0);
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.textColor1));
                break;
            case 1:
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.textColor3));
                holder.tvName.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg0);
                break;
            case 2:
                holder.tvName.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.defaultBlue));
                holder.tvName.setBackgroundResource(R.drawable.shape_lablegroup_lable_bg2);
                break;
        }
        if (TextUtils.isEmpty(preSelectedGoodsCode)) {
            if (!goods.isAdded()) {
                holder.itemView.setOnClickListener(v -> {
                    if (mOnClick != null) {
                        mOnClick.oneLableClick(goods);
                    }
                    for (Goods temp : data) {
                        if (temp.getCode() == goods.getCode()) {//当前
                            temp.setLableStatus(2);
                        } else {
                            if (temp.isAdded()) {
                                temp.setLableStatus(1);
                            } else {
                                temp.setLableStatus(0);
                            }
                        }
                    }
                    notifyDataSetChanged();
                });
            }
        }

    }



    public void setNewData(List<Goods> goods) {
        data.clear();
        data.addAll(goods);
        notifyDataSetChanged();
    }

    public List<Goods> getData() {
        return data;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setOnClick(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public void setPreSelectedGoodsCode(String preSelectedGoodsCode) {
        this.preSelectedGoodsCode = preSelectedGoodsCode;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
        }
    }

    public interface OnClick {
        void oneLableClick(Goods goods);
    }


}
